package com.glodon.paas.monitor.server.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DateFormatUtil {

	public static final Logger		LOGGER					= LoggerFactory.getLogger(DateFormatUtil.class);

	public static final String		DAFAULT_DATE_FORMAT		= "yyyy-MM-dd";

	public static final String		DAFAULT_DATETIME_FORMAT	= "yyyy-MM-dd HH:mm:ss";

	private static SimpleDateFormat	dateFormat				= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	public static String format(Date date) {
		return dateFormat.format(date);
	}

	public static String format(Long date) {
		return dateFormat.format(date);
	}

	public static Date parse(String source) {
		try {
			return dateFormat.parse(source);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public static SimpleDateFormat getFormatInstance(String pattern) {
		if (pattern == null || pattern.trim().length() == 0) {
			pattern = DAFAULT_DATE_FORMAT;
		}
		return new SimpleDateFormat(pattern);
	}

	public static Date parseStringDate(String date, String format) {
		try {
			return getFormatInstance(format).parse(date);
		} catch (Exception e) {
			LOGGER.error("解析[" + date + "]错误！", e);
			return null;
		}
	}

	public static String getIntervalDate(int interval, String format) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, interval);
		String intervalDate = new SimpleDateFormat(format).format(cal.getTime());
		return intervalDate;
	}

	public static String getIntervalHour(int interval) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.HOUR_OF_DAY, interval);
		String intervalDate = new SimpleDateFormat(DAFAULT_DATETIME_FORMAT).format(cal.getTime());
		return intervalDate;
	}

	public static String getIntervalMinute(int interval) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MINUTE, interval);
		String intervalDate = new SimpleDateFormat(DAFAULT_DATETIME_FORMAT).format(cal.getTime());
		return intervalDate;
	}

	public static void main(String[] args) {
		System.out.println(getIntervalMinute(5));
	}
}
